'use strict';

// 我的钉钉
let corpID = 'dingc6f6abc21a0d46b735c2f4657eb6378f';
let secret = 'o62WizIR9PC_2Rc4qxHAR3GODK1xo6BpZiA22mgprhg4oqVJbEF3HzjWfnRl0_nF';

// 经纬钉钉
corpID = 'ding3a8c9da236345ba135c2f4657eb6378f';
secret = 'x-DPygCbyLALX9dsV70jLkihwOxHSEH16M_b7c7J7eBk3OClXwWBCCcW0oVE5B7f';


module.exports = app => {
  let access_token = null;

  async function curl(url, param) {
    //access_token = com.token
    const { model: { Company } } = app
    let row = await Company.findOne({
      where: {
        code: 'jinwin'
      },
    })
    var ts = (new Date().getTime() - new Date(row.created_at).getTime()) / 1000
    if (!row.token || ts > 7100) {
      //重新获取token
      const ret = await app.curl(`https://oapi.dingtalk.com/gettoken?corpid=${row.corpid}&corpsecret=${row.secret}`, {
        method: 'GET',
        dataType: 'json',
      });
      row.token = ret.data.access_token;
      row.save()
    }
    access_token = row.token


    const data = Object.assign({
      access_token,
    }, param);

    const ret = await app.curl(`https://oapi.dingtalk.com${url}`, {
      method: 'GET',
      dataType: 'json',
      data: access_token == null ? undefined : data,
    });
    const res = ret.data;
    if (res.errcode) {
      throw new Error(`${url}请求失败！错误代码:${res.errcode} 错误信息: ${res.errmsg}`);
    }
    return res;
  }

  return {
    // 获取token
    async gettoken(corpID, secret) {
      // https://oapi.dingtalk.com/gettoken?corpid=ding3a8c9da236345ba135c2f4657eb6378f&corpsecret=x-DPygCbyLALX9dsV70jLkihwOxHSEH16M_b7c7J7eBk3OClXwWBCCcW0oVE5B7f
      const ret = await curl(`/gettoken?corpid=${corpID}&corpsecret=${secret}`);
      access_token = ret.access_token;
      return ret;
    },

    auth: {
      // 获取通讯录权限 
      // https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.5hnvyC&treeId=385&articleId=106091&docType=1#s0
      async scopes() {
        return await curl('/auth/scopes');
      },
    },

    // 部门
    department: {
      // https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.HYQphy&treeId=385&articleId=106817&docType=1#s0
      async list() {
        return await curl('/department/list');
      },
      // 获取部门详情 https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.jG7Nc8&treeId=385&articleId=106817&docType=1#s1
      async get(id) {
        return await curl('/department/get', { id });
      },
    },

    // 用户
    user: {
      // https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.G5qB56&treeId=385&articleId=106816&docType=1#s5
      async simplelist(param) {
        return await curl('/user/simplelist', param);
      },
      // https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.G5qB56&treeId=385&articleId=106816&docType=1#s6
      async list(param) {
        return await curl('/user/list', param);
      },
      //https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7386797.0.0.nTmEmV&source=search&treeId=370&articleId=104969&docType=1
      async getuserinfo(param) {
        return await curl('/user/getuserinfo', param)
      }
    },
  };
};
